<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 机器人聊天对话消息
 *
 * admin-RobotConversationMessage
 *
 * @Apidoc\Title("机器人聊天对话消息")
 * @Apidoc\Group("api")
 */
class RobotConversationMessage extends BaseController
{
    /**
     * 新增
     *
     * admin-RobotConversationMessage-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\RobotConversationMessage::create($data['robot_conversation_message']);
            return success();
        } else {
            $options = [];
            $robot_conversation_message__robot_conversation_id_option = \app\common\model\RobotConversation::column('name as name, id as id');
            $options['robot_conversation_message__robot_conversation_id_options'] = $robot_conversation_message__robot_conversation_id_option;
            $robot_conversation_message__is_positive_reviews_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_conversation_message__is_positive_reviews_options'] = $robot_conversation_message__is_positive_reviews_option;
            $robot_conversation_message__robot_id_option = \app\common\model\Robot::column('name as name, id as id');
            $options['robot_conversation_message__robot_id_options'] = $robot_conversation_message__robot_id_option;
            $robot_conversation_message__is_event_submission_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_conversation_message__is_event_submission_options'] = $robot_conversation_message__is_event_submission_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-RobotConversationMessage-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\RobotConversationMessage::update($data['robot_conversation_message']);
            return success();
        } else {
            $data = \app\common\model\RobotConversationMessage::find(input('robot_conversation_message__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['robot_conversation_message__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $robot_conversation_message__robot_conversation_id_option = \app\common\model\RobotConversation::column('name as name, id as id');
            $options['robot_conversation_message__robot_conversation_id_options'] = $robot_conversation_message__robot_conversation_id_option;
            $robot_conversation_message__is_positive_reviews_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_conversation_message__is_positive_reviews_options'] = $robot_conversation_message__is_positive_reviews_option;
            $robot_conversation_message__robot_id_option = \app\common\model\Robot::column('name as name, id as id');
            $options['robot_conversation_message__robot_id_options'] = $robot_conversation_message__robot_id_option;
            $robot_conversation_message__is_event_submission_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_conversation_message__is_event_submission_options'] = $robot_conversation_message__is_event_submission_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-RobotConversationMessage-delete
     */
    public function delete()
    {
        \app\common\model\RobotConversationMessage::destroy(input('robot_conversation_message__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-RobotConversationMessage-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_robot_conversation_message');
        $query->join('db_robot_conversation', 'db_robot_conversation.id = db_robot_conversation_message.robot_conversation_id');
        $query->join('db_robot', 'db_robot.id = db_robot_conversation_message.robot_id');
        if (input('robot_conversation_message__answer_time_start', '') || input('robot_conversation_message__answer_time_end', '')) {
            $robot_conversation_message__answer_time_start = input('robot_conversation_message__answer_time_start', '', 'search_in');
            $robot_conversation_message__answer_time_end = input('robot_conversation_message__answer_time_end', '', 'search_in');
            $robot_conversation_message__answer_time_start = $robot_conversation_message__answer_time_start ? strtotime($robot_conversation_message__answer_time_start) : 0;
            $robot_conversation_message__answer_time_end = $robot_conversation_message__answer_time_end ? strtotime($robot_conversation_message__answer_time_end) + 24 * 60 * 60 - 1 : time();
            $query->where('db_robot_conversation_message.answer_time', 'between', [$robot_conversation_message__answer_time_start, $robot_conversation_message__answer_time_end]);
        }
        if (input('robot_conversation_message__event_id', '') !== '') {
            $query->where('db_robot_conversation_message.event_id', '=', input('robot_conversation_message__event_id', '', 'search_in'));
        }
        if (input('robot_conversation_message__is_event_submission', '') !== '') {
            $query->where('db_robot_conversation_message.is_event_submission', '=', input('robot_conversation_message__is_event_submission', '', 'search_in'));
        }
        if (input('robot__id', '') !== '') {
            $query->where('db_robot.id', '=', input('robot__id', '', 'search_in'));
        }
        if (input('robot_conversation__id', '') !== '') {
            $query->where('db_robot_conversation.id', '=', input('robot_conversation__id', '', 'search_in'));
        }
        $query->where('db_robot_conversation_message.delete_time', '=', null);
        $query->where('db_robot.delete_time', '=', null);
        $query->where('db_robot_conversation.delete_time', '=', null);
        $query->field(['db_robot_conversation_message.id' => 'robot_conversation_message__id']);
        $query->field(['db_robot_conversation_message.user_questions' => 'robot_conversation_message__user_questions']);
        $query->field(['db_robot_conversation_message.machine_answer' => 'robot_conversation_message__machine_answer']);
        $query->field(['db_robot_conversation_message.referenced_files' => 'robot_conversation_message__referenced_files']);
        $query->field(['db_robot_conversation_message.is_positive_reviews' => 'robot_conversation_message__is_positive_reviews']);
        $query->field(['db_robot_conversation_message.answer_time' => 'robot_conversation_message__answer_time']);
        $query->withAttr('robot_conversation_message__answer_time', function ($value, $data) {
            return date('Y-m-d H:i:s', $value);
        });
        $query->field(['db_robot_conversation_message.event_id' => 'robot_conversation_message__event_id']);
        $query->field(['db_robot_conversation_message.is_event_submission' => 'robot_conversation_message__is_event_submission']);
        $query->field(['db_robot_conversation_message.event_details' => 'robot_conversation_message__event_details']);
        $query->field(['db_robot_conversation_message.user_submission' => 'robot_conversation_message__user_submission']);
        $query->field(['db_robot_conversation_message.similarity' => 'robot_conversation_message__similarity']);
        $query->field(['db_robot.name' => 'robot__name']);
        $query->field(['db_robot_conversation.name' => 'robot_conversation__name']);
        $query->order('db_robot_conversation_message.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        View::assign($data);
        $options = [];
        $robot_conversation_message__is_event_submission_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['robot_conversation_message__is_event_submission_options'] = $robot_conversation_message__is_event_submission_option;
        $robot__id_option = \app\common\model\Robot::column('name as name, id as id');
        $options['robot__id_options'] = $robot__id_option;
        $robot_conversation__id_option = \app\common\model\RobotConversation::column('name as name, id as id');
        $options['robot_conversation__id_options'] = $robot_conversation__id_option;
        $robot_conversation_message__is_positive_reviews_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['robot_conversation_message__is_positive_reviews_options'] = $robot_conversation_message__is_positive_reviews_option;
        $robot_conversation_message__is_event_submission_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['robot_conversation_message__is_event_submission_options'] = $robot_conversation_message__is_event_submission_option;
        View::assign($options);
        return View::fetch();
    }
}